package com.abbas.abchat.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.abbas.abchat.R;
import com.abbas.abchat.entity.Item;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * @author abbas
 * @description: TODO
 * @date 2022/4/16 22:00
 */
public class ItemRecycleAdapter extends RecyclerView.Adapter<ItemRecycleAdapter.ViewHolder> {

    private Context mContext;
    private List<Item> items;
    private OnRecycleViewItemClickListener itemClickListener;

    public ItemRecycleAdapter(Context mContext, List<Item> items) {
        this.mContext = mContext;
        this.items = items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

    public void setItemClickListener(OnRecycleViewItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        Item item = items.get(position);

        Glide.with(mContext)
                .load(item.getMapId())
                .into(holder.img);
        holder.name.setText(item.getName());

        holder.ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick(holder,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return items == null ? 0 : items.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{

        LinearLayout ll;
        ImageView img;
        TextView name;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ll = itemView.findViewById(R.id.ll);
            img = itemView.findViewById(R.id.img);
            name = itemView.findViewById(R.id.name);
        }
    }

}
